package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsShipmentOrderService;
import net.sf.json.JSONArray;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.service.WmsShipmentOrderDetailedService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.Page;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;






@RestController
@RequestMapping("/wmsshipmentorderdetailed")
@Api(value = "/wmsshipmentorderdetailed", description = "关于发运订单明细操作")
public class  WmsShipmentOrderDetailedController extends BaseController<WmsShipmentOrderDetailedService,WmsShipmentOrderDetailed> {

    @ApiOperation(value = "订单添加", response = JSONObject.class, notes = "添加发运订单明细")
    @PostMapping(value = "add")
    public @ResponseBody
    JSONObject addWmsPurchaseOrderDetailed(@RequestBody WmsShipmentOrderDetailed entity, HttpSession session) {
        entity.setId(UUIDUtils.uuid());
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        entity.setCkid(ckid);
        try {
            baseService.baseInsert(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单增加成功", "", JSONObject.fromObject(entity).toString());

        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单增加成功", e.getMessage(), JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            Boolean a = baseService.deleteById(entity.getId());
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单增加失败", e.getMessage(), "");
        } catch (Exception e) {
            Boolean b = baseService.deleteById(entity.getId());
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单增加失败", e.getMessage(), "");
        }

    }

    @ApiOperation(value = "发运订单明细编辑", response = JSONObject.class, notes = "编辑发运订单明细")
    @PostMapping(value = "edit")
    public @ResponseBody
    JSONObject edit(@RequestBody WmsShipmentOrderDetailed entity, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        try {
            baseService.baseUpdateMx(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单明细修改成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单明细修改成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单明细修改失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单明细修改失败", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "发运订单明细删除", response = JSONObject.class, notes = "查询输入</br>{\"ids\":\"明细id集合\",\"id\":\"订单id\"}")
    @PostMapping(value = "delete")
    public @ResponseBody
    JSONObject delete(@RequestBody SetAndModifyModel setModel, HttpSession session) {

        try {
            baseService.baseDeleteByPrimaryKey(setModel.getIds(),setModel.getId());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单明细删除成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单明细删除成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单明细删除失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单明细删除失败", e.getMessage(), "");
        }
    }

    /**
     * 欠料明细汇总表
     * @param
     * @param map
     * @return
     */
    @ApiOperation(value = "欠料明细汇总表查询", response = JSONObject.class, notes = "欠料明细汇总表查询")
    @PostMapping(value = "getQlCollect")
    public  @ResponseBody ResultPage<WmsShipmentOrderDetailed> getQlCollect(@RequestBody Map map){
        Page<WmsShipmentOrderDetailed> list=null;
        try {
            list=baseService.getQlCollect((List)map.get("ids"),map);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return renderPage(list);
    }

    @ApiOperation(value = "导出欠料明细汇总表", response = JSONObject.class, notes = "查询输入</br>{\"ids\":\"明细id集合\",\"hpbh\":\"货品编号\",\"bzmc\":\"包装名称\",\"fybh\":\"SO编号\",\"khddh\":\"客户订单号\"}")
    @PostMapping(value = "getOutPutList")
    public JSONObject getOutPutList(HttpServletResponse response, @RequestBody Map map, HttpSession session) throws IOException {

        String filename = "欠料明细汇总表导出";

        List<String> ids = (List) map.get("ids");//发运明细id
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("欠料明细汇总表导出", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsShipmentOrderOutModel> list = null;
        list = baseService.getOutPutList(ids, map);

        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsShipmentOrderOutModel.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }



    @ApiOperation(value = "编辑时查找明细集合", response = JSONObject.class, notes = "编辑时查找明细集合,{\"fydid\":\"发运订单id\",\"page\":0,\"rows\":10}")
    @PostMapping(value = "getDetailList")
    public @ResponseBody
    ResultPage getDetailList(HttpSession session, @RequestBody Map map) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        Page<WmsShipmentOrderDetailed> list =null;
        try {
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                list = baseService.getDetailList((String)map.get("fydid"),hzid,ckid,(Integer)map.get("page"),(Integer)map.get("rows"));
            }
        } catch (ServiceException e) {
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return renderPage(list);
    }
}
